﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;
using MogreNewt;

namespace WastedMechanics
{
    public class ClimbEdge : GameObject
    {
        public Vector3 Direction;
        public Body Body;
        public Vector3 A, B;        
       
        public ClimbEdge(Vector3 a, Vector3 b)
            : base()             
        {
            A = a;
            B = b;
            Vector3 edgeCenter = (a + b) * 0.5f;
            Quaternion edgeOrientation = Vector3.UNIT_X.GetRotationTo((a - b).NormalisedCopy);
            float edgeWidth = (a - b).Length * 0.5f;
            Direction = (a - b).CrossProduct(Vector3.UNIT_Y).NormalisedCopy;

            ConvexCollision collision = new MogreNewt.CollisionPrimitives.Cylinder(
                Engine.NewtonWorld,
                0.2f,
                edgeWidth * 2,
                Engine.GetUniqueBodyId());

            Body = new Body(Engine.NewtonWorld, collision);
            collision.Dispose();
            Body.SetMassMatrix(0, Vector3.ZERO);
            Body.SetPositionOrientation(edgeCenter, edgeOrientation);
            Body.UserData = this;
            Body.MaterialGroupID = Engine.MaterialManager.Materials["ClimbEdge"]; 
        }

        public override void Update()
        {
        }

        public override Vector3 Position
        {
            get
            {
                return Body.Position;
            }
            set
            {
                Body.SetPositionOrientation(value, Body.Orientation);
            }

        }

        public override Quaternion Orientation
        {
            get
            {
                return Body.Orientation;
            }
            set
            {
                Body.SetPositionOrientation(Body.Position, value);
            }
        }

        public override void Destroy()
        {
            Body.Dispose();
            Body = null;

            base.Destroy();
        }     
    }
}
